/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */


package com.LibreGeoSocial.App;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.util.Log;


public class GesturesHandler implements OnGesturePerformedListener
{
	
	private GestureLibrary mLibrary;
	
	private GenericList mList;
	
	private Hashtable<Object, Object> tGestures = null;
	public void initGestures (GenericList list, int gesture)
	{
		mList = list;
		
        mLibrary = GestureLibraries.fromRawResource(mList, gesture);
        if (!mLibrary.load()) {
            mList.finish();
        }
 
        GestureOverlayView gestures = (GestureOverlayView) mList.findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);
	}
	
	public void setGestures (Hashtable<Object, Object> gestures)
	{
		tGestures = gestures;
		
	}
	

	public void onGesturePerformed (GestureOverlayView overlay, Gesture gesture) {
		
		
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
		if (predictions.size() > 0) {
			if (predictions.get(0).score > 1.0) {
				String action = predictions.get(0).name;
				
				Log.e("LGS:", "Gesto");
				
				
				if (tGestures != null)
				{
					Enumeration<Object> e = tGestures.keys();
					
					while (e.hasMoreElements()) {
						
						Object obj = e.nextElement();
						
						if (obj.equals(action))
						{		
							Method m = null;
							m = (Method) tGestures.get(obj);							
							
							try {
	
								m.invoke((Object[])null,(Object[])null);
																
							} catch (IllegalArgumentException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (IllegalAccessException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (InvocationTargetException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}
					}
				}
				else
				{
					Log.e("LGS:", "Lista Vacia");
				}
				
			}
		}
	}
	
}
